package com.tinama.jedis01.demo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lgc
 * @createDate 2020/10/10 - 17:47
 */
public class NewShenXIao {
    public static void main(String[] args){
        Data data = new Data();
        new Thread(()->{for (int i=0;i<10;i++) {
            try {
                data.printA();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        },"生产者A").start();
        new Thread(()->{for (int i=0;i<10;i++) {
            try {
                data.printB();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        },"消费者B").start();
        new Thread(()->{for (int i=0;i<10;i++) {
            try {
                data.printC();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        },"生产者C").start();
//        new Thread(()->{for (int i=0;i<10;i++) {
//            try {
//                data.decrement();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//        },"消费者D").start();
    }

    static class Data{
        /**
        *
        */
        private int number=1;
        private Lock lock = new ReentrantLock();
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();
        public void printA() throws InterruptedException {
            lock.lock();
            try {
                while (number!=1){
                    condition1.await();
                }
                number=2;
                System.out.println(Thread.currentThread().getName()+"生产了并唤醒了B");
                this.condition2.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }
        public void printB() throws InterruptedException {
            lock.lock();
            try {
                while (number!=2){
                    condition2.await();
                }
                number=3;
                System.out.println(Thread.currentThread().getName()+"生产了并唤醒了C");
                this.condition3.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }

        public void printC() throws InterruptedException {
            lock.lock();
            try {
                while (number!=3){
                    condition3.await();
                }
                number=1;
                System.out.println(Thread.currentThread().getName()+"生产了并唤醒了A");
                this.condition1.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }

    }
}
